﻿using System.Windows;
using esquireclockscreensaver.Utils;

namespace esquireclockscreensaver.Behaviors
{
    public sealed class CloseViewBehavior
    {
        public static readonly DependencyProperty CloseViewProperty = DependencyProperty.RegisterAttached("CloseView", typeof(bool), typeof(CloseViewBehavior), 
                                                                        new PropertyMetadata(false, OnCloseViewChanged));

        public static bool GetCloseView(DependencyObject obj)
        {
            return (bool)obj.GetValue(CloseViewProperty);
        }

        public static void SetCloseView(DependencyObject obj, bool value)
        {
            obj.SetValue(CloseViewProperty, value);
        }

        private static void OnCloseViewChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
           if((bool)args.NewValue)
           {
               (dependencyObject as Window).Do(window => window.Close());
           }
        }
    }
}
